//
//  RemindViewController.m
//  EMMApp
//
//  Created by Mac on 16/5/11.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "RemindViewController.h"
#import "NoticeCell.h"
#import "RemindDetailsViewController.h"         //提醒详情
#import "GetRemindListApi.h"

@interface RemindViewController ()<UITableViewDelegate,UITableViewDataSource>

//定义tableView
@property (nonatomic, strong) UITableView *remindListTableView;

//数据源数组
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation RemindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNavigationController];
    _remindListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _remindListTableView.delegate = self;
    _remindListTableView.dataSource = self;
    _remindListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _remindListTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_remindListTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backPushNotification:) name:@"isBackPushNotification" object:nil];
}

- (void)backPushNotification:(NSNotification *)notification {
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _dataArr = [[NSMutableArray alloc] init];
    [self postGetRemindListApi];
}

#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"提醒";
//    [self configLeftBtn];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, 54 ,44)];
    [backBtn addTarget:self
                action:@selector(notBtnClick:)
      forControlEvents:UIControlEventTouchUpInside];
    [backBtn setBackgroundColor:[UIColor clearColor]];
    [backBtn setImage:[UIImage imageNamed:@"ico_back"] forState:UIControlStateNormal];
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    if (EMM_WIDTH <= 320) {
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }else if (EMM_WIDTH > 375){
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    }else{
        backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    }
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)notBtnClick:(UIButton *)btn {
    if (_isNot == YES) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

#pragma mark - 提醒列表Api
- (void)postGetRemindListApi {
    [self showHudInWindow:@"正在加载请稍等..."];
    GetRemindListApi *api = [[GetRemindListApi alloc] initWith];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetRemindListApi *result = (GetRemindListApi *)request;
        if (result.isSuccess) {
            GetRemindList *get = result.resultData;
            for (RemindList *list in get.remindList) {
                [_dataArr addObject:list];
            }
            if (_dataArr.count > 0) {
                _remindListTableView.hidden = NO;
            }else {
                _remindListTableView.hidden = YES;
                UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, EMM_HEIGHT/2-100, EMM_WIDTH, 20)];
                l.text = @"当前没有记录";
                l.textAlignment = NSTextAlignmentCenter;
                l.font = [UIFont systemFontOfSize:14];
                [self.view addSubview:l];
            }
        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [_remindListTableView reloadData];
        [self hideHud];
    } failure:^(YTKBaseRequest *request) {
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
    }];
}

#pragma mark - UITableView 代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"NoticeCellId";
    
    NoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"NoticeCell" owner:self options:nil] lastObject];
        
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 89, EMM_WIDTH-20, 1)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    [cell addSubview:imageView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //提醒类别：1、客户维护；2、客户关怀；3、员工关怀；4、认筹/认购/签约通知；5、审核结果
    if (_dataArr.count == 0) {
        return nil;
    }
    RemindList *list = [_dataArr objectAtIndex:indexPath.row];
    if ([list.isRead isEqualToString:@"已读"]) {
        cell.noticeYuanDianLabel.hidden = YES;
        cell.noticeNale1Label.text = list.remindCategory;
    }else if ([list.isRead isEqualToString:@"未读"]) {
        cell.noticeYuanDianLabel.hidden = NO;
        cell.noticeYuanDianLabel .backgroundColor = [UIColor colorFromHexCode:@"#ff7200"];
        cell.noticeNaleLabel.text = list.remindCategory;

    }
    cell.noticeTimerLabel.text = list.remindDate;
    cell.noticLabel.text = list.remindContent;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataArr.count > 0) {
        NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
        NSString *projectCode = [userdf objectForKey:@"projectCode"];
        RemindList *list = [_dataArr objectAtIndex:indexPath.row];
        RemindDetailsViewController *rvc = [[RemindDetailsViewController alloc] init];
        rvc.remindCategoryId = list.remindCategoryId;
        rvc.nameTitleString = list.remindCategory;
        rvc.isNot = YES;
        rvc.projectCode = projectCode;
        [self.navigationController pushViewController:rvc animated:YES];
    }
   
}




@end
